import axios from 'axios'
import { useMain } from "../store/index";
import router from "../routes/index"
import { useRouter } from 'vue-router';


const hdAxios = axios.create();//新建一个axios实例
hdAxios.defaults.timeout = 15000;
hdAxios.defaults.baseURL = import.meta.env.VITE_BASE_API_HD
// hdAxios.defaults.withCredentials = true; //设置cross跨域 并设置访问权限 允许跨域携带cookie信息
// hdAxios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
// hdAxios.defaults.headers['Content-Type'] = 'Content-Type:application/json; charset=UTF-8'; //配置axios的基础请求头
hdAxios.defaults.headers['Content-Type'] = 'application/json; charset=UTF-8';

//请求拦截器
hdAxios.interceptors.request.use((config) => {
    const useMainStore = useMain();
    const token = useMainStore.authToken_hd ? useMainStore.authToken_hd : '';
    if (token) {
        // 在发起请求前，给所有请求的头部添加token
        config.headers['Authorization'] = token;
    }
    return config
}, (error) => {
    return Promise.reject(error)
})



//响应拦截器
hdAxios.interceptors.response.use(response => {
    let res = response.data;
    if (!response.data) {
        res = response;
    }
    // console.log('response', response);
    // console.log('res',res);

    // if (res && res.isSuccess === true) {//请求成功时的处理
    if (res ) {//请求成功时的处理
        return Promise.resolve(res);
    } else {//请求失败时的处理
    }
    return Promise.reject(res)
}, async (error) => {
    if (error.response && error.response.data) {

    } else if (error.resultCode && error.resultMsg) {

    } else {

    }
    return Promise.reject(error)
})
export default hdAxios

/**
* 封装get方法
* @param url
* @param params
* @returns {Promise}
*/

export function get(url: any, params: Object = {}) {
    return new Promise((resolve, reject) => {
        hdAxios.get(url, {
            params: params
        })
            .then(response => {
                resolve(response)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
* 封装post请求
* @param url
* @param data
* @returns {Promise}
*/

export function post(url: any, data: Object = {}) {
    return new Promise((resolve, reject) => {
        hdAxios.post(url, data)
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
    })
}

/**
* 封装delete方法
* @param url
* @param params
* @returns {Promise}
*/

export function doDelete(url: any, params: Object = {}) {
    return new Promise((resolve, reject) => {
        hdAxios.delete(url, {
            params: params
        })
            .then(response => {
                resolve(response.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
* 封装patch请求
* @param url
* @param data
* @returns {Promise}
*/

export function patch(url: any, data: Object = {}) {
    return new Promise((resolve, reject) => {
        hdAxios.patch(url, data)
            .then(response => {
                resolve(response.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
* 封装put请求
* @param url
* @param data
* @returns {Promise}
*/

export function put(url: any, data: Object = {}) {
    return new Promise((resolve, reject) => {
        hdAxios.put(url, data)
            .then(response => {
                resolve(response.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}